<template>
  <div>
    <h2>当前和为：{{ sum }}</h2>
    <h2>当前和放大10倍：{{ bigSum }}</h2>
    <h2>我是{{ name }},今年{{ age }}</h2>
    <select v-model.number="n">
      <option :value="1">1</option>
      <option :value="2">2</option>
      <option :value="3">3</option>
    </select>
    <button @click="increment">++</button>
    <button @click="decrement">--</button>
    <button @click="incrementOdd">当前和为奇数再加</button>
    <button @click="decrementOdd">等2秒再加</button>
  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
export default {
  name: "Count",
  data() {
    return {
      n: 1,
    };
  },
  computed: {
    // sum() {
    //   return this.$store.state.sum;
    // },
    // name() {
    //   return this.$store.state.name;
    // },
    // age() {
    //   return this.$store.state.age;
    // },
    // 借助mapState生成计算属性，从state读取数据，（对象方法
    // ...mapState({ sum1: "sum", name1: "name", age1: "age" }),
    // 借助mapState生成计算属性，从state读取数据，（数组方法
    ...mapState(["sum", "name", "age"]),

    // bigSum() {
    //   return this.$store.getters.bigSum;
    // },
    // 借助mapGetters生成计算属性，从getters读取数据，（对象方法
    // ...mapGetters({ bigSum: "bigSum" }),
    // 借助mapGetters生成计算属性，从getters读取数据，（数组方法
    ...mapGetters(["bigSum"]),
  },
  methods: {
    increment() {
      this.$store.commit("PLUS", this.n);
    },
    decrement() {
      this.$store.dispatch("reduce", this.n);
    },
    incrementOdd() {
      if (this.$store.state.sum % 2) {
        this.$store.dispatch("plusOdd", this.n);
      }
    },
    decrementOdd() {
      setTimeout(() => {
        this.$store.dispatch("plusWait", this.n);
      }, 2000);
    },
  },
  mounted() {
    const x = mapState({ sum1: "sum", name1: "name", age1: "age" });
    console.log(x);
  },
};
</script>

<style lang="css">
button {
  margin-left: 5px;
}
</style>